/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lostreseditores.vista;

import com.lostreseditores.logica.LogicaUsuarioLocal;
import com.lostreseditores.modelo.RolUsuario;
import com.lostreseditores.modelo.Usuario;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.password.Password;
import org.primefaces.component.selectonemenu.SelectOneMenu;

/**
 *
 * @author DILOVE
 */
@ManagedBean(name = "usuarioVista")
@RequestScoped
public class UsuarioVista {

    @EJB
    private LogicaUsuarioLocal logicaUsuario;

    private InputText txtIdUsuario;
    private Password txtPassword;
    private SelectOneMenu cmbRoles;
    private CommandButton btnIngresar;
    private CommandButton btnModulo;

    private ArrayList<SelectItem> itemsRoles = null;

    public CommandButton getBtnModulo() {
        return btnModulo;
    }

    public void setBtnModulo(CommandButton btnModulo) {
        this.btnModulo = btnModulo;
    }

    
    
    public ArrayList<SelectItem> getItemsRoles() {
            try {
                itemsRoles = new ArrayList();
                List<RolUsuario> lista;
                FacesContext context = FacesContext.getCurrentInstance();
                ExternalContext extContext = context.getExternalContext();
                Usuario usuarioLogeado = (Usuario) extContext.getSessionMap().get("usuarioSacpe");
                System.out.println("Usuario Logeado " + usuarioLogeado.getNombreusuario());
                Usuario nuevo = logicaUsuario.consultarPorId(usuarioLogeado.getIdUsuario());
                
                lista = nuevo.getRolUsuarioList();
                System.out.println("Roles "+lista.size());
                try {
                    for (int i = 0; i < lista.size(); i++) {
                        if (lista.get(i).getEstado()) {
                            System.out.println("Role activo");
                            itemsRoles.add(new SelectItem(lista.get(i).getIdRol().getNombrerol(), lista.get(i).getIdRol().getNombrerol()));
                        }
                    }
                } catch (Exception ex) {
                    Logger.getLogger(UsuarioVista.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (Exception ex) {
                Logger.getLogger(UsuarioVista.class.getName()).log(Level.SEVERE, null, ex);
            }
        return itemsRoles;
    }

    public void setItemsRoles(ArrayList<SelectItem> itemsRoles) {
        this.itemsRoles = itemsRoles;
    }

    public SelectOneMenu getCmbRoles() {
        return cmbRoles;
    }

    public void setCmbRoles(SelectOneMenu cmbRoles) {
        this.cmbRoles = cmbRoles;
    }

    public InputText getTxtIdUsuario() {
        return txtIdUsuario;
    }

    public void setTxtIdUsuario(InputText txtIdUsuario) {
        this.txtIdUsuario = txtIdUsuario;
    }

    public Password getTxtPassword() {
        return txtPassword;
    }

    public void setTxtPassword(Password txtPassword) {
        this.txtPassword = txtPassword;
    }

    public CommandButton getBtnIngresar() {
        return btnIngresar;
    }

    public void setBtnIngresar(CommandButton btnIngresar) {
        this.btnIngresar = btnIngresar;
    }

    public void ingresar_action() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext extContext = context.getExternalContext();
        String url = "";

        try {
            Usuario usr = new Usuario();
            usr.setIdUsuario(Integer.parseInt(txtIdUsuario.getValue().toString()));
            usr.setClaveusuario(txtPassword.getValue().toString());
            Usuario logeado = logicaUsuario.ingresar(usr);
            if (logeado != null) {
                url = extContext.encodeActionURL(context.getApplication().getViewHandler().getActionURL(context, "/sacpeindex.xhtml"));
                extContext.getSessionMap().put("usuarioSacpe", logeado);
                extContext.redirect(url);
            }

        } catch (NumberFormatException ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", "La Identificación Debe Ser Numérica!"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", ex.getMessage()));
        }

    }

    public void cerrarSesion_action() {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            ExternalContext extContext = context.getExternalContext();
            extContext.getSessionMap().remove("usuarioSacpe");
            String url = extContext.encodeActionURL(
                    context.getApplication().getViewHandler().getActionURL(context, "/index.xhtml"));
            extContext.redirect(url);
        } catch (IOException ex) {
            Logger.getLogger(UsuarioVista.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void abrirModulo(){
        try {
            String modulo = cmbRoles.getValue().toString();
            FacesContext context = FacesContext.getCurrentInstance();
            ExternalContext extContext = context.getExternalContext();
            String url = extContext.encodeActionURL(
                    context.getApplication().getViewHandler().getActionURL(context, "/"+modulo+"/"+modulo+".xhtml"));
            extContext.redirect(url);
        } catch (IOException ex) {
            Logger.getLogger(UsuarioVista.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
